﻿#region Copyright Notice
// This file is part of GestionPedago.
// <description>
// Copyright (C) 2012 ENI Ecole Informatique
// 
// GestionPedago is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// <program> is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects.DataClasses;
using GestionPedago.EntityContext.EntityOPTools;
using GestionPedago.Persistence.OPManager;
using Persistence.OPManager;

namespace GestionPedago.EntityContext
{
    public partial class Promotion : EntityObject
    {
       public List<PlanningECF> GetListePlanificationsECF(bool orderByDateEcfAsc = true) {
           try
           {
               IQueryable<PlanningECF> query = EntityConnection.GetContext().PlanningECF;
               /*if (this.CodePromotion != null)
               {*/
               query = query.Where(it => it.CodePromotion == this.CodePromotion);
              /* }
               else
               {
                   query.Where(it => it.CodePromotion == null);
               }*/
               if (orderByDateEcfAsc)
               {
                   query = query.OrderBy(it => it.DateECF);
               }
               else
               {
                   query = query.OrderByDescending(it => it.DateECF);
               }
               /**query.Select(it => it);*/

               List<PlanningECF> lst = new List<PlanningECF>();
               List<PlanningECF> listePlanningECF = query.ToList();
               foreach (PlanningECF pe in listePlanningECF)
               {
                   if (pe.CodePromotion == this.CodePromotion)
                   {
                       lst.Add(pe);
                   }
               }

               return lst;
           }
           catch (Exception ex)
           {
               throw ex;
           }
       }

      public List<ECF> GetListeECFAPasser(){
        try
        {
            List<ECF> lst = new List<ECF>();
            List<PlanningECF> lstDejaPlanifie = this.GetListePlanificationsECF();
            List<ECF> listeECFAPasser = FormationOPManager.GetFormationByCode(this.CodeFormation).GetListeECFAPasser();
            foreach (ECF e in listeECFAPasser)
            {
                PlanningECF pe = new PlanningECF();
                pe.ECF = e;
                if(!lstDejaPlanifie.Contains(pe,new PlanningECFComparerOnECF())){
                    lst.Add(e);
                }
                EntityConnection.GetContext().Detach(pe);
            }
            return lst;
        }
        catch (Exception ex)
        {
            throw ex;
        }
      }

      /* public List<Cours> GetListeCours() {
        try{
           List<Cours> lst = new List<Cours>();
           foreach(Cours c in this.Cours) {
               lst.Add(c);
           }
           return lst;
        }
        catch (Exception ex)
        {
            throw ex;
        }
       }*/

      public void AppliquerPlanificationsECFATousLesStagiaires()
      {
          foreach (PlanningECF pe in this.PlanningECF)
          {
              foreach (PlanningIndividuelFormation pif in this.PlanningIndividuelFormation)
              {
                  bool ecfAPlanifier = true;
                  foreach(PlanningECFDetail ped in pif.PlanningECFDetail){
                      if (ped.PlanningECF.IdECF == pe.IdECF)
                      {
                          ecfAPlanifier = false;
                      }
                 }
                if (ecfAPlanifier)
                {
                    PlanningECFDetail newPed = new PlanningECFDetail();
                    newPed.PlanningECF = pe;
                    newPed.PlanningIndividuelFormation = pif;
                    PlanningECFDetailOPManager.AjouterPlanningECFDetail(newPed,pe.IdECF,pe.CodePromotion);
                }
              }
          }
      }

       public void MiseAjour(){
           try{
                EntityConnection.GetContext().SaveChanges();
           }
           catch (Exception ex)
           {
               throw ex;
           }
       }
    }
}
